package com.atzly.community.datasource.util;


/**
 * 获取SpringContext上下文
 * 包含当前线程的appId,tenantId,dbId
 */
public class ContextHelper {

//    private static final ThreadLocal<Deque<ServiceContext>> threadLocal = new NamedThreadLocal<Deque<ServiceContext>>("hjy-dynamic-datasource") {
//        @Override
//        protected Deque<ServiceContext> initialValue() {
//            return new ArrayDeque<>();
//        }
//    };

    private static final ThreadLocal<ServiceContext> threadLocal = new ThreadLocal<>();
    private static final ThreadLocal<Boolean>  specifiedLocal = new ThreadLocal<>();
    private static final ThreadLocal<String>  dsLocal = new ThreadLocal<>();
    public static void setServiceContext(ServiceContext serviceContext) {
        threadLocal.set(serviceContext);
    }

    public static void setSpecified(Boolean specified) {
        specifiedLocal.set(specified);
    }

    public static Boolean getSpecified( ) {
        return specifiedLocal.get() == null ? false : specifiedLocal.get();
    }
    public static void setDsLocal(String specified) {
        dsLocal.set(specified);
    }

    public static String getDsLocal( ) {
        return dsLocal.get() == null ? "mysql" : dsLocal.get();
    }

    public static void main(String[] args) {
//        ContextHelper.setSpecified(true);
        System.out.println(getSpecified());
    }

    public static void clear() {
        threadLocal.remove();
    }

    public static ServiceContext getServiceContext() {
        ServiceContext serviceContext = threadLocal.get() ;
//        Long tenantId = AuthContext.getTenantId();
//        if (tenantId != 0L && serviceContext == null) {
//            serviceContext = new ServiceContext();
//            serviceContext.setTenantId(tenantId);
//            setServiceContext(serviceContext);
//        }
        return serviceContext == null ? new ServiceContext() : serviceContext;
    }


}
